package com.ogawa.base.utils;

public class HexUtils {
	/**
	 * 二进制转16进制
	 * @param b
	 * @param offset
	 * @param count
	 * @param haveSpace
	 * @return
	 */
	public static String getHexString(byte[] b, int offset, int count,
                                      boolean haveSpace) {
		String string = "";
		for (int i = offset; i < offset + count; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			if (haveSpace)
				hex += " ";
			string = string + hex.toUpperCase();
		}
		return string;
	}
	// 16 进制转2进制
	public static byte[] hexToByte(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	/**
	 *
	 * @param data
	 * @param position
	 * @return
	 */

	public static String getByteInPosition(byte data,int position){
		return String.valueOf(((data  >>> position) & 0x1));
	}

	/**
	 *
	 * @param data
	 * @param bytePosition buffer[3] --对应block0.要扣除前面的7B(BYTE 0) B1(BYTE 1) OC(BYTE 2)  例如文档里面byte4 此处即为3
	 * @param position
	 * @return
	 */
	public static String getByteInPosition(String data,int bytePosition ,int position){
		byte[] buffer = HexUtils.hexToByte(data);
		return String.valueOf(((buffer[bytePosition] >>> position) & 0x1));
	}



	// 获取一个字节的8个bit======
	/**
	 * 
	 * @param b
	 * @return
	 */
	public static String getBitString(byte b, int[] count) {
		byte[] array = new byte[8];
		String newString = "";
		for (int i = 7; i >= 0; i--) {
			array[i] = (byte) (b & 1);
			b = (byte) (b >> 1);
		}
		// 其他位置清零。
		for (int k = 0; k < count.length; k++) {
			array[count[k]] = 0;
		}
		// 拼接字符串
		for (int j = 0; j < array.length; j++) {
			newString = newString + array[j];
		}
		return newString;
	}

	// 而进制转字符串数据
	public static String getBinaryString(byte[] b) {
		String data = null;
		for (byte K : b) {
			data = data + K;
		}
		return data;
	}

	// 16进制转2进制字符串、
	public static String hexString2binaryString(String hexString) {
		if (hexString == null || hexString.length() % 2 != 0)
			return null;
		String bString = "", tmp;
		for (int i = 0; i < hexString.length(); i++) {
			tmp = "0000"
					+ Integer.toBinaryString(Integer.parseInt(
							hexString.substring(i, i + 1), 16));
			bString += tmp.substring(tmp.length() - 4);
		}
		return bString;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	// 16进制加法计算

	public static String addHexString(String hex) {
		
		String[] hexs=new String[hex.length()/2];
		for(int i=0;i<hex.length()/2;i++){
			hexs[i]=hex.substring(i*2,i*2+2);
		}
       
		long total = 0;
		for (int i = 0; i < hexs.length; i++) {
			long getLong = Long.parseLong(hexs[i], 16);
			total = getLong + total;
		}
		String hexString = Long.toHexString(total);
		return hexString;
	}
	// 16进制转ASCII
	public static String convertHexToString(String hex) {
		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();
		// 49204c6f7665204a617661 split into two characters 49, 20, 4c...
		for (int i = 0; i < hex.length() - 1; i += 2) {
			// grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			// convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			// convert the decimal to character
			sb.append((char) decimal);
			temp.append(decimal);
		}
		return sb.toString();
	}

	
	//7B0123 AB CB BC NM M      ..... 23
	// 16進制轉10進制
	public static int[] hexStringToAlgorism(String hex) {
		hex = hex.toUpperCase();
		int length = hex.length() / 2;
		int[] datas = new int[length];
		// String [] hexs=new String[length];
		for (int i = 0; i < length; i++) {
			String hex1 = hex.substring(i * 2, i * 2 + 2);
			int max = hex1.length();
			int result = 0;
			for (int j = max; j > 0; j--) {
				char c = hex1.charAt(j - 1);
				int algorism = 0;
				if (c >= '0' && c <= '9') {
					algorism = c - '0';
				} else {
					algorism = c - 55;
				}
				result += Math.pow(16, max - j) * algorism;
			}

			datas[i] = result;

		}
		//
		return datas;
	}

	// // 10进制转16进制
	public static String algorismToHex(String a, int byteCount) {

		String hex = Integer.toHexString(Integer.valueOf(a));
		if (hex.length() == byteCount * 2) {
			//
		} else if (hex.length() < byteCount * 2) {
			hex = toReturn0(byteCount * 2 - hex.length()) + hex;
		} else {
			hex = hex.substring(hex.length() - byteCount * 2, hex.length());
		}
		return hex.toUpperCase();

	}


	// // 10进制转16进制
	public static String intToHex(int a) {
		int byteCount=1;
		String hex = Integer.toHexString(a);
		if (hex.length() == byteCount * 2) {
			//
		} else if (hex.length() < byteCount * 2) {
			hex = toReturn0(byteCount * 2 - hex.length()) + hex;
		} else {
			hex = hex.substring(hex.length() - byteCount * 2, hex.length());
		}
		return hex.toUpperCase();

	}

	// 返回0的个数

	public static String toReturn0(int i) {
		String str = "";
		for (int j = 0; j < i; j++) {
			str = str + "0";
		}
		return str;

	}

}
